﻿using UnityEngine;
using System.Collections;


public class Game : UnitySingleton<Game> {


	public Game_Status status;
	public Game_Status Status//游戏状态
	{
		get{return status;}
		set
		{
			status=value;
			UIManager.Instance.UIChange(status);
		}
	}

	public int currentID=1;//当前关数
    private Player player;//玩家

	void Start () {
        player = FindObjectOfType<Player>();
		Status = Game_Status.Ready;
        event_manager.Instance.add_listener("LevelUp", LevelUp);
        event_manager.Instance.add_listener("prop", OnProp);
	}

    //掉落
    void OnProp(object udata)
    {
        Vector3 pos = (Vector3)udata;
        if (Random.Range(0, 10) < 5)
            Instantiate(Resources.Load<GameObject>("Prefabs/Game/propHP"), pos, Quaternion.identity);
        else
            Instantiate(Resources.Load<GameObject>("Prefabs/Game/propAT"), pos, Quaternion.identity);
    }

    void Update()
	{
		if (Input.GetKeyDown (KeyCode.Escape))
			Application.Quit ();
	}
	
    //游戏开始
	public void GameStart()
	{
		Status = Game_Status.Start;
		player.Fly ();
		LoadLevels ();
        event_manager.Instance.add_listener("OnFail", OnFail);
	}

    //重新游戏
	public void ReGame()
	{
        UnityEngine.SceneManagement.SceneManager.LoadScene(0);
	}

    //加载关卡
	public void LoadLevels()
	{
		if ( currentID>LevelManager.Instance.levels.Count)
		{
            UIManager.Instance.UIend();
			return;
		}
		LevelManager.Instance.LoadLevel (currentID);
	}

    //下一关
	private void LevelUp(object udata)
	{
		currentID++;
		LoadLevels();
	}

   //游戏失败
    private void OnFail(object udata)
    { 
		Status = Game_Status.Over;
		UnitManager.Instance.StopGame ();
	
	}


}
